<template>
  <div class="home-module__featured container">
    <div class="featured-header">
      <div class="title">{{ datas.cate.label }}</div>
      <div class="category">
        <div
          v-for="(item, index) in datas.cate.children.slice(0, 5)"
          :key="index"
          class="li"
          :class="{ active: item.value == active }"
          @click="onChange(item.value)"
        >
          {{ item.label }}
        </div>
      </div>
      <div class="more" @click="goMore(datas.cate.value)">
        更多<i class="el-icon-arrow-right"></i>
      </div>
    </div>
    <div class="featured-body">
      <div
        class="featured-advert"
        :style="{
          background: featuredAdvertBgColor[getFeaturedCount()]
        }"
      >
        <div class="advert-title">{{ datas.cate.label }}</div>
        <div class="advert-line"></div>
        <div class="advert-course" v-if="$util.isDef(datas.advert.id)">
          <EduCourseImage
            :image="datas.advert.cover_url"
            :height="89"
            lazy
            @click="goCourse(datas.advert.id)"
          >
          </EduCourseImage>
          <div class="content">
            <div class="title" @click="goCourse(datas.advert.id)">{{ datas.advert.title }}</div>
            <div class="parallel">
              <div class="num">{{ datas.advert.sale_count }}人在学</div>
              <div class="price" v-if="datas.advert.user_price > 0">
                {{ datas.advert.user_price | formatMoney }}
              </div>
              <div v-else class="free">免费</div>
            </div>
          </div>
        </div>
      </div>
      <div class="featured-list">
        <div class="item" v-for="(item, index) in datas.list.slice(0, 8)" :key="index">
          <EduCourseImage
            :image="item.cover_url"
            :course-type="item.course_type"
            :border-radius="6"
            :height="120"
            lazy
            class="hvr-float"
            @click="goCourse(item.id)"
          >
          </EduCourseImage>

          <div class="content">
            <div class="title" @click="goCourse(item.id)">{{ item.title }}</div>
            <div class="parallel">
              <div class="num">{{ item.sale_count }}人在学</div>
              <div class="price" v-if="item.user_price > 0">
                {{ item.user_price | formatMoney }}
              </div>
              <div v-else class="free">免费</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import EduCourseImage from '@/components/EduCourseImage'
export default {
  name: 'Featured',
  components: {
    EduCourseImage
  },
  props: {
    datas: {
      type: Object,
      default: () => {}
    },
    // 下标
    moduleIndex: {
      type: Number,
      default: 0
    },
    // 出现第一次的下标
    minIndex: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      active: 0,
      courseIcon: {
        1: require('@/assets/images/course/course_video_icon.png'),
        2: require('@/assets/images/course/course_live_icon.png'),
        4: require('@/assets/images/course/course_class_icon.png')
      },

      featuredAdvertBgColor: [
        '#A9C8FF',
        '#AAACFF',
        '#F9AEFF',
        '#FFCCA3',
        '#FAE093',
        '#FFB1A4',
        '#FFB2C9',
        '#A6F09B',
        '#9CF0FD',
        '#9DF7D9'
      ]
    }
  },
  computed: {
    getFeaturedCount() {
      return _ => {
        const balance = this.minIndex % 10
        return (this.moduleIndex - balance) % 10
      }
    }
  },
  methods: {
    onChange(value) {
      this.active = value
      // 切换分类课程
      this.$api.getHomeTemplateCatecourse(
        {
          cate: value
        },
        res => {
          const { data } = res
          this.datas.list = data
        }
      )
    },
    // 跳转更多
    goMore(value) {
      this.$router.push({
        path: '/course',
        query: {
          category: value
        }
      })
    },
    // 跳转课程
    goCourse(id) {
      if (id == 0) return
      this.$router.push({
        path: '/course/' + id
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/styles/hover.scss';
.home-module__featured {
  margin-bottom: 30px;
  padding: 30px 0;
  background-color: #ffffff;
  .featured-header {
    display: flex;
    align-items: center;
    padding: 0 15px;
    position: relative;
    &::before {
      content: '';
      width: 3px;
      height: 22px;
      position: absolute;
      left: 0;
      top: 50%;
      margin-top: -11px;
      @include edu-color-primary(background-color);
    }
    .title {
      font-size: 18px;
      font-weight: bold;
      color: #333333;
      line-height: 22px;
      max-width: 198px;
      @include ellipsis;
    }
    .category {
      flex: 1;
      min-width: 0;
      display: flex;
      align-content: center;
      .li {
        height: 40px;
        font-size: 14px;
        font-weight: 400;
        color: #333333;
        line-height: 40px;
        margin-left: 40px;
        position: relative;
        cursor: pointer;
        max-width: 126px;
        @include ellipsis;
        &.active {
          @include edu-color-primary;
          &::after {
            content: '';
            width: 26px;
            height: 3px;
            border-radius: 2px;
            position: absolute;
            left: 50%;
            bottom: 0;
            margin-left: -13px;
            @include edu-color-primary(background-color);
          }
        }
      }
    }
    .more {
      font-size: 14px;
      font-weight: 400;
      color: #aeaeae;
      line-height: 16px;
      cursor: pointer;
    }
  }
  .featured-body {
    padding: 0 15px;
    margin-top: 30px;
    display: grid;
    grid-column-gap: 20px;
    grid-template-columns: 210px 1fr;
    .featured-advert {
      height: 390px;
      padding: 40px 13px 0;
      .advert-title {
        font-size: 18px;
        font-weight: bold;
        color: #ffffff;
        line-height: 27px;
        @include ellipsis;
      }
      .advert-line {
        margin: 30px 0;
        width: 30px;
        height: 1px;
        background: #ffffff;
      }
      .advert-course {
        padding: 14px 10px 20px;
        background: #ffffff;
        border-radius: 2px;
        .content {
          margin-top: 10px;
          .title {
            font-size: 14px;
            font-weight: 500;
            color: $text-color-import;
            cursor: pointer;
            @include ellipsisLine(2);
          }
          .parallel {
            margin-top: 12px;
            display: flex;
            align-items: center;
            .num {
              flex: 1;
              min-width: 0;
              font-size: 14px;
              font-weight: 400;
              color: $text-color-auxiliary;
            }
            .price,
            .free {
              font-size: 12px;
              font-weight: 500;
              margin-left: 5px;
            }
            .price {
              color: $text-color-price;
            }
            .free {
              color: $text-color-free;
            }
          }
        }
      }
    }
    .featured-list {
      display: grid;
      grid-template-columns: repeat(4, minmax(0px, 1fr));
      gap: 30px 25px;
      .item {
        .content {
          margin-top: 6px;
          .title {
            font-size: 16px;
            font-weight: 500;
            color: $text-color-import;
            cursor: pointer;
            @include ellipsis;
          }
          .parallel {
            margin-top: 15px;
            display: flex;
            align-items: center;
            .num {
              flex: 1;
              min-width: 0;
              font-size: 14px;
              font-weight: 400;
              color: $text-color-auxiliary;
            }
            .price,
            .free {
              font-size: 18px;
              font-weight: 500;
              margin-left: 5px;
            }
            .price {
              color: $text-color-price;
            }
            .free {
              color: $text-color-free;
            }
          }
        }
      }
    }
  }
}
</style>
